package com.jspgou.core.manager.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UrlPathHelper;

import com.jspgou.cms.entity.ShopAdmin;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.ShopAdminMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.RequestUtils;
import com.jspgou.core.dao.LogDao;
import com.jspgou.core.entity.Admin;
import com.jspgou.core.entity.Log;
import com.jspgou.core.entity.Member;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.AdminMng;
import com.jspgou.core.manager.LogMng;
import com.jspgou.core.manager.MemberMng;
import com.jspgou.core.manager.UserMng;
@Service
@Transactional
public class LogMngImpl implements LogMng {
    @Transactional(readOnly=true)
	public Pagination getPage(Integer category, Long siteId,
			String adminName, String title, String ip, int pageNo, int pageSize) {
		Pagination pagination;
		if(StringUtils.isBlank(adminName)){
			pagination=logDao.getPage(category, siteId, null, title, ip, pageNo, pageSize);
		}else{
			ShopAdmin admin=adminMng.findByName(adminName);
			if(admin!=null){
				pagination=logDao.getPage(category, siteId, admin.getId(), title, ip, pageNo, pageSize);
			}else{
				pagination=new Pagination(1, pageSize, 0, new ArrayList<Log>(0));
			}
		}
		return pagination;
	}

    @Transactional(readOnly=true)
	public Log findById(Long id) {
		return logDao.findById(id);
	}

    public Log save(Integer category, Website website,ShopAdmin admin,
			String url, String ip, Date date, String title, String content) {
		Log log = new Log();
		log.setWebsite(website);
		log.setAdmin(admin );
		log.setCategory(category);
		log.setIp(ip);
		log.setTime(date);
		log.setUrl(url);
		log.setTitle(title);
		log.setContent(content);
		save(log);
		return log;
	}
    
	public Log operating(HttpServletRequest request, String title,
			String content) {
		Website website=CmsUtils.getWebsite(request);
		ShopAdmin admin=CmsUtils.getAdmin(request);
		String ip=RequestUtils.getIpAddr(request);
		UrlPathHelper helper=new UrlPathHelper();
		String url=helper.getOriginatingRequestUri(request);
		Date date=new Date();
		Log log=save(Log.OPERATING, website, admin, url, ip, date,
				title, content);
		return log;
	}

	

	public Log loginFailure(HttpServletRequest request, ShopAdmin admin) {
		String ip=RequestUtils.getIpAddr(request);
		UrlPathHelper helper=new UrlPathHelper();
		String url=helper.getOriginatingRequestUri(request);
		Date date=new Date();
		String name = "";
		if(admin==null){
			
		}else if(admin.getAdmin()!=null){
			name = admin.getAdmin().getUsername();
		}else{
			name = admin.getUsername();
		}
		Log log=save(Log.LOGIN_FAILURE, null, admin, url, ip, date, Log.LOGIN_FAILURE_TITLE,"username= "+name);
		return log;
	}

	public Log loginSuccess(HttpServletRequest request, ShopAdmin admin) {
		Website website=CmsUtils.getWebsite(request);
		String ip=RequestUtils.getIpAddr(request);
		UrlPathHelper helper=new UrlPathHelper();
		String url=helper.getOriginatingRequestUri(request);
		Date date=new Date();
		Log log=save(Log.LOGIN_SUCCESS, null, admin, url, ip, date, Log.LOGIN_SUCCESS_TITLE, "username="+admin.getAdmin().getUsername());
		return log;
	}

	public Log loginOut(HttpServletRequest request ,ShopAdmin admin){
		Website website=CmsUtils.getWebsite(request);
		String ip=RequestUtils.getIpAddr(request);
		UrlPathHelper helper=new UrlPathHelper();
		String url=helper.getOriginatingRequestUri(request);
		Date date=new Date();
		Log log=save(Log.LOGIN_OUT,website,admin,url,ip,date,
				Log.LOGIN_OUT_TITLE,"username= "+admin.getAdmin().getUsername());
		return log;
	}
	public Log save(Log bean) {
		return logDao.save(bean);
	}

	public Log deleteById(Long id) {
		Log bean=logDao.findById(id);
		return logDao.deleteById(id);
	}

	public Log[] deleteByIds(Long[] ids) {
		Log[] logs=new Log[ids.length];
		for(int i=0;i<ids.length;i++){
			logs[i]=logDao.deleteById(ids[i]);
		}
		return logs;
	}

	public int deleteBatch(Integer category, Long siteId, Integer days) {
		Date date=null;
		if( days!=null&&days>0){
			Calendar calendar=Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, -days);
			date=calendar.getTime();
		}
		return logDao.deleteBatch(category, siteId, date);
	}

	@Autowired
	private LogDao logDao;
	@Autowired
	private ShopAdminMng adminMng;
	@Autowired
	private UserMng userMng;
	@Autowired
	private MemberMng memberMng;
}
